using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DataAccessLayer;
using SecurityAccessLayer;

namespace WebUI
{
    public partial class WebUIMasterPage : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            btnLogin.Text = "Login";
            if (!Page.IsPostBack)
            {
                if (Session[Keys.Session.FirstLoad] == null)
                {
                    Session[Keys.Session.FirstLoad] = 1;
                    Session[Keys.Session.ValidState] = ValidState.Undefined;
                    Session[Keys.Session.LogonState] = LogonState.Undefined;
                    Response.Redirect(Keys.Pages.Default);
                }
                Session[Keys.Session.UserName] = HttpContext.Current.User.Identity.Name.Split(new char[] { '\\' })[1];
            }

            LogonState logonState = (LogonState)Session[Keys.Session.LogonState];

            if (logonState != LogonState.LoggedIn && !Request.FilePath.EndsWith("Default.aspx"))
            {
                Response.Redirect(Keys.Pages.Default);
            }
            if (logonState == LogonState.LoggedIn)
            {
                lblUserName.Text = "Welcome " + (string)Session[Keys.Session.UserName];
                btnLogin.Text = "Logout";
            }
            else if (logonState == LogonState.Undefined)
            {
                lblUserName.Text = "Please logon below.";
                Session[Keys.Session.UserName] = HttpContext.Current.User.Identity.Name.Split(new char[] { '\\' })[1]; ;
                Session[Keys.Session.ValidState] = ValidState.Undefined;
            }
            else
            {
                logonState = LogonState.NotLoggedIn;
                lblUserName.Text = "Please logon below.";
                Session[Keys.Session.UserName] = HttpContext.Current.User.Identity.Name.Split(new char[] { '\\' })[1]; ;
                Session[Keys.Session.ValidState] = ValidState.NotValid;
                Session[Keys.Session.LogonState] = LogonState.NotLoggedIn;
                btnLogin.Text = "Login";
            }

            #region Set Side Nav 
            if ((LogonState)Session[Keys.Session.LogonState] == LogonState.LoggedIn)
            {
                if (SAL.CanUserDoStuff((int)Session[Keys.Session.UserID], DoStuffType.Manager))
                {
                    pnlManagerLinks.Visible = true;
                }
                else
                {
                    pnlManagerLinks.Visible = false;
                }
                if (SAL.CanUserDoStuff((int)Session[Keys.Session.UserID], DoStuffType.Consultant))
                {
                    pnlConsultantLinks.Visible = true;
                }
                else
                {
                    pnlConsultantLinks.Visible = false;
                }
            }
            else
            {
                pnlConsultantLinks.Visible = false;
                pnlManagerLinks.Visible = false;
            }

            #endregion
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            int userID = -1;
            string userName = ((string)Session[Keys.Session.UserName]);
            LogonState logonState = (LogonState)Session[Keys.Session.LogonState];

            if (logonState != LogonState.LoggedIn)
            {
                if (SAL.IsValidatedUser(userName, out userID) == ValidState.Valid)
                {
                    Session[Keys.Session.ValidState] = ValidState.Valid;
                    Session[Keys.Session.LogonState] = LogonState.LoggedIn;
                    Session[Keys.Session.UserID] = userID;
                    lblUserName.Text = "Welcome " + (string)Session[Keys.Session.UserName];
                    btnLogin.Text = "Logout";
                    Response.Redirect(Keys.Pages.Default);
                }
                else
                {
                    Session[Keys.Session.ValidState] = ValidState.NotValid;
                    Session[Keys.Session.LogonState] = LogonState.NotLoggedIn;
                    Session[Keys.Session.UserID] = null;
                    Response.Redirect(Keys.Pages.Default);
                }
            }
            else if (logonState == LogonState.LoggedIn)
            {
                SAL.LogoffUser(userName);
                Session[Keys.Session.LogonState] = LogonState.NotLoggedIn;
                Response.Redirect(Keys.Pages.Default);
            }
            else
            {
                SAL.LogoffUser(userName);
                Session[Keys.Session.ValidState] = ValidState.NotValid;
                Session[Keys.Session.LogonState] = LogonState.NotLoggedIn;
                Response.Redirect(Keys.Pages.Default);
            }
        }
    }
}
